﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Utilities;
using System.IO;

namespace t_Utilities
{
    /// <summary>
    /// Test out the logger test.
    /// </summary>
    [TestClass]
    public class t_Logger
    {
        public t_Logger()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestInitialize]
        public void InitTestFramework()
        {
            /// Reset the test framework before every test
            Logger.ResetTestLogging();
        }

        [TestMethod]
        public void TestDefaultLogging()
        {
            Logger.LogErrorMessage("test", "this is a test of all that has gone wrong");
        }

        [TestMethod]
        public void TestWriteToFile()
        {
            /// Test writing a message to a file
            FileInfo f = new FileInfo("test-log.txt");
            if (f.Exists)
            {
                f.Delete();
            }
            Logger.UseFileLogger(f);
            Logger.LogErrorMessage("dude", "yo my holy cow!");
            FileInfo f1 = new FileInfo("test-log.txt");
            Assert.IsTrue(f1.Exists, "Log file does not exist");
            Assert.IsTrue(f1.Length > 25, "Output file is too small");
            long len = f1.Length;

            Logger.LogErrorMessage("dude", "yo my holy cow!");
            FileInfo f2 = new FileInfo("test-log.txt");
            Assert.IsTrue(f2.Length >= len * 2 - 5, "Lenght didn't double when we wrote the second log message");
        }

        [TestMethod]
        public void TestWriteException()
        {
            /// Test writing a message to a file
            FileInfo f = new FileInfo("test-log.txt");
            if (f.Exists)
            {
                f.Delete();
            }
            Logger.UseFileLogger(f);

            try
            {
                int i = 10;
                i = i + 1;
                throw new SystemException("ops - bad things have happened");
            } catch (Exception e)
            {
                Logger.LogErrorMessage("yo", "failed!", e);
            }
        }
    }
}
